#####################################
##REPLICATION FOR MCGHEE, ET AL.,####
##"A PRIMARY CAUSE OF PARTISANSHIP"##
#####################################

#Table A6 (Appendix)#

#Load Data#
library(Zelig)
library(sandwich)
library(MatchIt)
library(WhatIf)
setwd("XXXX") #ENTER DIRECTORY PATHNAME IN PLACE OF XXXX

legyrr <- read.csv("legislator primaries.rep.csv", header=TRUE)

#generate election year counter#
legyrr$yrcnt <- (legyrr$elec-1992)/2

#drop ideowt var, which is not used for this table#
legyrr$ideowt <- NULL

#drop missing data#
legyrr <- na.omit(legyrr)

#define years & states as factors for FE regression#
legyrr$icpsrst <- as.factor(legyrr$icpsrst)
legyrr$yrcnt <- as.factor(legyrr$yrcnt)

##Matching: Republicans##
#Semi-closed#
legyrr.semicl <- legyrr[legyrr$semicl==1 | legyrr$closed==1,]
m.out <- matchit(semicl ~ pvote + yrcnt,
	data=legyrr.semicl, method="nearest", discard="hull.both")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ semicl + distance + pvote + 
	icpsrst + yrcnt, robust=list(method="vcovHAC"), 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#Semi-open#
legyrr.semiop <- legyrr[legyrr$semiop==1 | legyrr$closed==1,]
m.out <- matchit(semiop ~ pvote + yrcnt,
	data=legyrr.semiop, method="nearest", discard="hull.both")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ semiop + distance + pvote + icpsrst + yrcnt, 
	robust=list(method="vcovHAC"), 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#Open#
legyrr.open <- legyrr[legyrr$open==1 | legyrr$closed==1,]
m.out <- matchit(open ~ pvote + yrcnt,
	data=legyrr.open, method="nearest", discard="hull.both")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ open + distance + pvote + yrcnt + icpsrst, 
	robust=list(method="vcovHAC"), 
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)

#Nonpartisan#
legyrr.nonpart <- legyrr[legyrr$nonpart==1 | legyrr$closed==1,]
m.out <- matchit(nonpart ~ pvote + yrcnt,
	data=legyrr.nonpart, method="nearest")
summary(m.out)
m.data <- match.data(m.out)
z.out <- zelig(pred_np ~ nonpart + distance + pvote +	icpsrst + yrcnt, 
	robust=list(method="vcovHAC"),
	model="ls", data=m.data)
summary(z.out)
length(m.data$icpsrst)


